public class board  {
    public static int [][]board= new int[20][10];
    public static int[]coordinates=new int [2];
    public static int[] variation;
    public static boolean possibleDown=true;
    public static int score=0;
    public static int lines=0;
    public static boolean full=true;
    public static int scorePerLine=10;
	public static int level=1;
    
    
    public static void createPento(){            //sets variation to a new random pento (needs to be called every time a new pento appears)
        variation=Pento.getPento();
        coordinates[0]=0;
        coordinates[1]=2-variation[12];
        //full=true;
        if (!possible())            //if the new pento cannot spawn the game is over
        {
            //System.out.println("true true true");
            game.gameover=true;
            timerx.frame.repaint();
            timerx.frame.removeKeyListener(timerx.myTester);

            HighscoreManager hs = new HighscoreManager();
            hs.addScore("Clemens",score);
            hs.addScore("Joelle",score);
            hs.addScore("Gaston",score);
            hs.addScore("Alex",score);
            hs.addScore("Alberto",score);
                    
            System.out.print(HighscoreManager.getHighscoreString());
            
            
        }
        else            //else we need to check if it has space to move down
        {
            coordinates[0]=coordinates[0]+1;
            if (possible())            
            {
                possibleDown=true;        
                coordinates[0]=coordinates[0]-1;    
                place();
            }
            else
            {
                coordinates[0]=coordinates[0]-1;    
                place();    
                possibleDown=false;
            }
        }
    }
    

    
        
        public static void checkAndPlace ()        //coordination method (quite useless... could be removed)
         {
             if (possible())
             {
                 place();            
            }                
         }    
         
         
         
        public static boolean possible()                //checks if the pento can be placed with these coordinates
         {
                 if (checkOn())
                 {
                     if(checkFree())
                     {
                         return true;
                     }
                     else
                     {
                         return false;
                     }
                 }
                 else
                 {
                     return false;
                 }
             
             
         }
        
        public static boolean checkOn ()            //checks if the whole pento will be on the board     (sub-method of "possible".... to avoid out of bounce errors)
    {
         for(int b=1; b<10; b=b+2)
         {
             if (0>coordinates[1]+variation[b+1] || coordinates[1]+variation[b+1] >= board[0].length || 0>coordinates[0]+variation[b]||coordinates[0]+variation[b]>=board.length) // took out: || 0>coordinates[1] || coordinates[1] >= board[0].length || 0>coordinates[0]||coordinates[0]>=board.length
             {
                 System.out.println("variation "+variation[0]+" =not on board");
                 return false;
             }
         }
        return true;
    }

        
    public static boolean checkFree ()            //checks if nothing will be overwritten (sub-method of "possible")
    {
        for (int a=1; a<10; a=a+2)
        {            
                
            if(board[coordinates[0]+variation[a]] [coordinates[1]+variation[a+1]]!=0)
            {
                        
            System.out.println("variation "+variation[0]+" =overwrites");
            /*
                 for(int b2=0; b2<9; b2++)
                 {
                     System.out.print(variation[b2]);
                }
                */
                 return false;
            
            }
        }
        return true;
    }
        
        
        
        
        public static void moveDown(){            //moves the pento down (if possible)
            remove();
            coordinates[0]=coordinates[0]+1;
            if (possible())
            {
                place();
                timerx.frame.repaint();
            }
            else
            {
                coordinates[0]=coordinates[0]-1;    
                place();    
                possibleDown=false;
                checkLines();
            }
        }
        
        
        
        public static void drop(){                //moves the current pento as far down as possible
            while (possibleDown)
            {
                moveDown();
            }
        }

        
        public static void moveLeft(){            //moves the pento to the left (if possible)
            remove();
            coordinates[1]=coordinates[1]-1;
            if (possible())
            {
                place();
                timerx.frame.repaint();
            }
            else
            {
                coordinates[1]=coordinates[1]+1;
                place();    
            }
        }
        
        public static void moveRight(){            //moves the pento to the right (if possible)
            remove();
            coordinates[1]=coordinates[1]+1;
            if (possible())
            {
                place();
                timerx.frame.repaint();
            }
            else
            {
                coordinates[1]=coordinates[1]-1;
                place();
            }
        }
        
        public static void turnCW(){            //rotates the pento clockwise
            int [] temp=new int[variation.length];
            System.arraycopy(variation, 0, temp, 0, variation.length);
            remove();
            variation[1]=temp[2]+temp[11]-temp[12];
            variation[3]=temp[4]+temp[11]-temp[12];
            variation[5]=temp[6]+temp[11]-temp[12];
            variation[7]=temp[8]+temp[11]-temp[12];
            variation[9]=temp[10]+temp[11]-temp[12];
            
            //y2 = (px + py - x1 - q)
                    
            variation[2]=temp[11]+temp[12]-temp[1];
            variation[4]=temp[11]+temp[12]-temp[3];
            variation[6]=temp[11]+temp[12]-temp[5];
            variation[8]=temp[11]+temp[12]-temp[7];
            variation[10]=temp[11]+temp[12]-temp[9];
            
            
            if (possible())
            {
                place();
                timerx.frame.repaint();
            }
            else
            {
                System.arraycopy(temp, 0, variation, 0, temp.length);
                place();
            }
        }
        
        public static void turnCCW(){            //rotates the pento counterclockwise
            int [] temp=new int[variation.length];
            System.arraycopy(variation, 0, temp, 0, variation.length);
            remove();
            variation[1]=temp[11]+temp[12]-temp[2];
            variation[3]=temp[11]+temp[12]-temp[4];
            variation[5]=temp[11]+temp[12]-temp[6];
            variation[7]=temp[11]+temp[12]-temp[8];
            variation[9]=temp[11]+temp[12]-temp[10];
            
            //y2 = (px + py - x1 - q)
                    
            variation[2]=temp[1]+temp[12]-temp[11];
            variation[4]=temp[3]+temp[12]-temp[11];
            variation[6]=temp[5]+temp[12]-temp[11];
            variation[8]=temp[7]+temp[12]-temp[11];
            variation[10]=temp[9]+temp[12]-temp[11];
            
            
            if (possible())
            {
                place();
                timerx.frame.repaint();
            }
            else
            {
                System.arraycopy(temp, 0, variation, 0, temp.length);
                place();
            }
        }
        
        
        public static void place (){            //places pento on board
         board[coordinates[0]+variation[1]][coordinates[1]+variation[2]]=variation[0];
         board[coordinates[0]+variation[3]][coordinates[1]+variation[4]]=variation[0];
         board[coordinates[0]+variation[5]][coordinates[1]+variation[6]]=variation[0];
         board[coordinates[0]+variation[7]][coordinates[1]+variation[8]]=variation[0];
         board[coordinates[0]+variation[9]][coordinates[1]+variation[10]]=variation[0];
             
    }

    public static void remove(){            //removes the pento from the board
         board[coordinates[0]+variation[1]][coordinates[1]+variation[2]]=0;
         board[coordinates[0]+variation[3]][coordinates[1]+variation[4]]=0;
         board[coordinates[0]+variation[5]][coordinates[1]+variation[6]]=0;
         board[coordinates[0]+variation[7]][coordinates[1]+variation[8]]=0;
         board[coordinates[0]+variation[9]][coordinates[1]+variation[10]]=0;
    }


    public static void printBoard()            //prints the board
    {
        System.out.println();
         for (int a=0; a<board.length; a++)
         {
            for(int b=0; b<board[0].length; b++)
            {
                System.out.print(board[a][b]);
            }
            
            System.out.println();             
        }
         System.out.println();
         
         
         
    }
    
    public static void printVariation(int[]variation)            //prints the variation (used for debugging)
    {
        System.out.println();
        for(int b=1; b<variation.length-2; b++)
            {
                System.out.print(variation[b]);
            }
            
            System.out.println();             
        
    }

    
    public static void checkLines()            //checks the board for full lines, deletes them, moves the rest down and counts the deleted lines
    {
        System.out.println("lines will be checked");
         for (int a=0; a<board.length; a++)
         {
            for(int b=0; b<board[0].length; b++)
            {
                if (board[a][b]==0)
                {
                    full=false;
                }
            }
            //System.out.println("line" + a + full);
            if (full)
            {
                for(int i=a; i>0; i--)
                {    
                    board[i]=board[i-1];
                    
                }
                board[0]=new int[board[0].length];
                lines++;
                score+=scorePerLine;
                System.out.println("deleted lines: "+lines);
                 System.out.println("your score: "+score);
				 levelCheck();
            }
            full=true;
                         
        }
        
        
    }
    
	public static  void levelCheck() {
		if (score>= Math.pow(level*10, 1.3)) {
			level++;
			game.delay = (int)(game.delay*0.7);
			game.myTimer.setDelay(game.delay);
		}	
	}	
	/*public int setDelay()	{
		game.delay=(int)(game.delay*0.9);
		return game.delay;
	}*/
   
        
        
 }